import { listofBills } from '../../../../api.js'
var app = getApp(); //获取App内部的信息
const date = new Date()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:'2018年8',
    datee: '',
    account_num:'',
    billing_num:'',
    billlist:[],
    page:1,
    last_page:'',
    current_page:'',
    data_time:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var aaa = year + '-' + month
    var that = this
    console.log(aaa)
    this.setData({
      datee: aaa
    })
    listofBills({ token: app.globalData.token, page: that.data.page}).then(res => {
      var account_num = res.data.data.account_num;//支出
      var billing_num = res.data.data.billing_num;//收入
      var lis = that.data.billlist.concat(res.data.data.data);//列表
      var last_page = res.data.data.last_page //最大页数  
      var current_page = res.data.data.current_page//当前页面
      if(res.data.code == 666){            
        that.setData({
          account_num: account_num,
          billing_num: billing_num,
          billlist: lis,
          last_page: last_page,
          current_page: current_page,
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }
else{
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var aaa = year + '年' + month
    this.setData({
      date: aaa
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var last_page = that.data.last_page; //最大页数
    var page = that.data.page //当前页数
    if (page >= last_page){
      wx.showToast({
          title: '没有更多数据啦~',
          icon: 'none',
          duration: 1500,
          mask: true
        })
    }else{
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      that.setData({
        page: page + 1
      })
      var page = that.data.page;
      listofBills({ token: app.globalData.token, page: page, start_date: that.data.start_date}).then(res => {
        var lis = that.data.billlist.concat(res.data.data.data);//列表
        var current_page = res.data.data.current_page//当前页面
        if (res.data.code == 666) {
          that.setData({
            billlist: lis,
          })
          wx.hideLoading();
        } else if (res.data.code == 111111) {
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/user/login/login',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindDateChange: function (e) {
    wx.showLoading({
      title: '正在加载',
    })
    var time = e.detail.value.replace('-', '年');
    var data_time = e.detail.value;
    var that = this
    that.setData({
      date: time,
      data_time: data_time,
      billlist:[],
      page:1
    })
    that.fetchlis(data_time)
  },
  fetchlis(time){
    listofBills({ token: app.globalData.token, page: this.data.page, start_date: time }).then(res => {
      wx.hideLoading()
      var that = this
      var account_num = res.data.data.account_num;//支出
      var billing_num = res.data.data.billing_num;//收入
      var lis = that.data.billlist.concat(res.data.data.data);//列表
      var last_page = res.data.data.last_page //最大页数  
      var current_page = res.data.data.current_page//当前页面
      if (res.data.code == 666) {
        that.setData({
          account_num: account_num,
          billing_num: billing_num,
          billlist: lis,
          last_page: last_page,
          current_page: current_page
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  bill_detailed:function(e){
    var ids = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/user/bill/detailed/detaileds?id='+ids,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})